package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_resource;

import cn.com.api.service.ApiU_resourceService;

import cn.com.util.*;


public class U_resourceBaseController {

	private static Logger logger = Logger.getLogger(U_resourceBaseController.class);
	
	@Autowired
	private ApiU_resourceService apiU_resourceService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_resource-list")
	public ModelAndView u_resource_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_resource_list.jsp");
		
		List<ApiU_resource> u_resources = apiU_resourceService.findU_resources();
		mv.addObject("u_resources", u_resources);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_resource-add-pre")
	public ModelAndView u_resource_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_resource_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_resource u_resource = apiU_resourceService.findU_resourceById(Integer.valueOf(id));
			mv.addObject("u_resource",u_resource);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_resource-add")
	public void u_resource_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String resource_id = request.getParameter("resource_id");
		String res_name = request.getParameter("res_name");
		String res_url = request.getParameter("res_url");
		String create_time = request.getParameter("create_time");
		String create_by = request.getParameter("create_by");
		String status = request.getParameter("status");
		String res_size = request.getParameter("res_size");
		String res_image = request.getParameter("res_image");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		String type = request.getParameter("type");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_resource u_resource = new ApiU_resource();
            /*
		    u_resource.setResource_id(Integer.valueOf(resource_id));
		    u_resource.setRes_name(res_name);
		    u_resource.setRes_url(res_url);
		    u_resource.setCreate_time(create_time);
		    u_resource.setCreate_by(create_by);
		    u_resource.setStatus(status);
		    u_resource.setRes_size(res_size);
		    u_resource.setRes_image(res_image);
		    u_resource.setExt_1(ext_1);
		    u_resource.setExt_2(ext_2);
		    u_resource.setExt_3(ext_3);
		    u_resource.setExt_4(ext_4);
		    u_resource.setExt_5(ext_5);
		    u_resource.setType(type);
	        */
			apiU_resourceService.insertU_resource(u_resource);
		}
		//edit
		else{
			ApiU_resource u_resource = apiU_resourceService.findU_resourceById(Integer.valueOf(id));
	        /*
		    u_resource.setResource_id(Integer.valueOf(resource_id));
		    u_resource.setRes_name(res_name);
		    u_resource.setRes_url(res_url);
		    u_resource.setCreate_time(create_time);
		    u_resource.setCreate_by(create_by);
		    u_resource.setStatus(status);
		    u_resource.setRes_size(res_size);
		    u_resource.setRes_image(res_image);
		    u_resource.setExt_1(ext_1);
		    u_resource.setExt_2(ext_2);
		    u_resource.setExt_3(ext_3);
		    u_resource.setExt_4(ext_4);
		    u_resource.setExt_5(ext_5);
		    u_resource.setType(type);
			*/
			apiU_resourceService.updateU_resource(u_resource);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_resource-delete")
	public void u_resource_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_resourceService.deleteU_resource(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



